<?php
    header("content-type:text/html;charset=utf-8");
    $config = include_once('../../public/config/config.php');
    include_once('../../public/func/db.php');
    include_once('../../public/func/shangchuan.func.php');
    $table = 'user';
    $filename = '../../public/upload';
    $_POST['ctime'] = time();
    $_POST['pic'] = shangchuan($_FILES['pic'],$filename);
    if($_POST['pic'] == '没有文件被上传'){
        $_POST['pic'] = 'default.jpg';
    }
    $unamepattern = "/\w/";
    $pwdpattern = "/^[a-z\d]{8,16}$/i";
    $emailpattern = "/^\w+(\.\w+)*@\w+(\.\w+)+$/";
    if(empty($_POST['nicname'])){
        die("<script>alert('昵称不能为空');window.location.href='adduser.php'</script>");
    }else if(!preg_match($unamepattern,$_POST['uname'])){
        die("<script>alert('用户名不符合数据类型');window.location.href='adduser.php'</script>");
    }else if(!preg_match($pwdpattern,$_POST['pwd'])){
        die("<script>alert('密码不符合数据类型');window.location.href='adduser.php'</script>");
    }else if(empty($_POST['sex'])){
        die("<script>alert('性别不能为空');window.location.href='adduser.php'</script>");
    }else if(!preg_match($emailpattern,$_POST['email'])){
        die("<script>alert('邮箱不符合数据类型');window.location.href='adduser.php'</script>");
    }
    $_POST['pwd'] = md5($_POST['pwd']);
    $res = insert($config,$table,$_POST);
    if(empty($res)){
        echo "<script>alert('添加成功');window.location.href='user.php'</script>";
    }else{
        echo "<script>alert('添加失败');window.location.href='adduser.php'</script>";
    }
?>